package org.lql.algo.codecrush.week007;

/**
 * @author: liangqinglong
 * @date: 2025-08-18 22:52
 * @description: 875. 爱吃香蕉的珂珂 <a href="https://leetcode.cn/problems/koko-eating-bananas/description/">...</a>
 **/
public class MinEatingSpeed {

	public int minEatingSpeed(int[] piles, int h) {
		int slow = 1;
		int fast = 1000000000;
		while (slow < fast) {
			int mid = slow + (fast - slow) / 2;
			int time = 0;
			for (int pile : piles) {
				// 每个小时都会吃掉N堆香蕉中的k根香蕉
				// 吃得完刚好，吃不完再吃一回
				time += pile % mid == 0 ? pile / mid : pile / mid + 1;
			}
			// 根据耗时调整速度
			if (time <= h) {
				fast = mid;
			} else {
				slow = mid + 1;
			}
		}
		return slow;
	}

	public static void main(String[] args) {
		MinEatingSpeed kokoEatingBananas = new MinEatingSpeed();
		int[] piles = {3, 6, 7, 11};
		int h = 8;
		System.out.println(kokoEatingBananas.minEatingSpeed(piles, h));
		piles = new int[]{30, 11, 23, 4, 20};
		h = 5;
		System.out.println(kokoEatingBananas.minEatingSpeed(piles, h));
		piles = new int[]{30, 11, 23, 4, 20};
		h = 6;
		System.out.println(kokoEatingBananas.minEatingSpeed(piles, h));
	}
}
